#!/bin/bash

currentdir=$(cd "$(dirname "$0")"; pwd)
echo "build directory: ${currentdir}."

echo -e "\nbuild pcre..."
if [ ! -d "$currentdir/build/linux" ]; then 
	mkdir -p "$currentdir/build/linux"
else
	rm -rf ${currentdir}/build/linux/*
fi

NDK="/opt/android-ndk-r25c"
# Only choose one of these, depending on your build machine...
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# Only choose one of these, depending on your device...
#export TARGET=aarch64-linux-android
export TARGET=armv7a-linux-androideabi
#export TARGET=i686-linux-android
#export TARGET=x86_64-linux-android
# Set this to your minSdkVersion.
export API=24
# Configure and build.
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip
#./configure --host $TARGET
#make



autoreconf
./configure --enable-utf8 --enable-shared=no CFLAGS=-fPIC --prefix=/usr/local --host $TARGET
make
make install DESTDIR=${currentdir}/build/linux

if [ ! -f "${currentdir}/build/linux/usr/local/lib/libpcre.a" ]; then exit 1; fi
rm -rf ${currentdir}/../../../lib/libpcr*.a
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcre.a" ${currentdir}/../../../lib
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcrecpp.a" ${currentdir}/../../../lib
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcreposix.a" ${currentdir}/../../../lib

make clean
rm -rf ${currentdir}/build/linux/*

echo "====== pcre build success! ======"
exit 0
